/*
 * Copyright (C) 2014 Nathan T
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package emergence.game.combat;

import emergence.entities.Mob;
import emergence.game.Game;
import emergence.game.Tickable;

/**
 *
 * @author Nathan T
 */
public class Combat implements Tickable {
    
    private Mob[] combatants;
    private int currentTurnIndex = 0;
    
    public Combat(Mob... combatants) {
        this.combatants = combatants;
        
        for (Mob mob : combatants) {
            mob.setCombat(true, this);
        }
        sortCombatants();
        Game.addTickable(this);
    }
    
    private void sortCombatants() {
        // Reorder the combatants to the correct order here
    }
    
    public Mob[] getCombatants() {
        return combatants;
    }
    
    @Override
    public void tick() {
        if (isFinished()) {
            end();
        }
        else if (combatants[currentTurnIndex].combatTurnOver()) {
            if (currentTurnIndex >= combatants.length) {
                currentTurnIndex = 0;
            }
            if (combatants[currentTurnIndex] != null) {
                combatants[currentTurnIndex].startCombatTurn();
            }
            else {
                incrementCurrentTurnIndex();
            }
            incrementCurrentTurnIndex();
        }
    }
    
    private void incrementCurrentTurnIndex() {
        currentTurnIndex++;
        if (currentTurnIndex >= combatants.length) {
            currentTurnIndex = 0;
        }
    }
    
    public boolean isFinished() {
        return isFriendsOnly();
    }
    
    protected boolean isFriendsOnly() {
        boolean friendsOnly = true;
        Mob firstMob = null;
        for(Mob mob : combatants) {
            if (firstMob == null && mob != null) {
                firstMob = mob;
            }
            else if (firstMob != null && mob != null) {
                friendsOnly = friendsOnly && firstMob.isFriendsWith(mob);
            }
        }
        return friendsOnly;
    }
    
    public void addCombatant(Mob newCombatant) {
        Mob[] oldComb = combatants;
        combatants = new Mob[oldComb.length + 1];
        for (int index = 0; index < oldComb.length; index++) {
            combatants[index] = oldComb[index];
        }
        combatants[combatants.length - 1] = newCombatant;
    }
    
    public void removeCombatant(Mob combatant) {
        for(int index = 0; index < combatants.length; index++) {
            if (combatants[index] == combatant) {
                combatants[index] = null;
            }
        }
    }
    
    public void end() {
        for(Mob mob : combatants) {
            if (mob != null) {
                mob.setCombat(false);
            }
        }
        Game.removeTickable(this);
    }
    
}
